/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package adee.qrlogin;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import qrlogin.common.QRLoginParam;

/*
 *
 * @author adee
 */
public class LoginInfo {
    private HashMap<String,QRLoginParam> params;
    private String xml= // debug
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<params>\n" +
"<param type=\"NORMAL\" name=\"username\" humanname=\"Felhasznalo nev\"></param>\n"+
"<param type=\"PASSWORD\" name=\"password\" humanname=\"Jelszo\"></param>\n"+
"</params>\n";

    public LoginInfo(String xml) {
        this.xml = xml;
        this.params = new HashMap<String, QRLoginParam>();
        parse();
    }

     public LoginInfo() {
        this.params = new HashMap<String, QRLoginParam>();
       // parse();
    }

    private void parse(){
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document dom = builder.parse(new InputSource(new StringReader(xml)));
            Element root = dom.getDocumentElement();
            NodeList items = root.getElementsByTagName("param");
            for (int i=0;i<items.getLength();i++){
                Node item = items.item(i);
                NamedNodeMap attributes = item.getAttributes();
                String name = attributes.getNamedItem("name").getNodeValue();
                String humanName = attributes.getNamedItem("humanname").getNodeValue();
                String type = attributes.getNamedItem("type").getNodeValue();
                String value = item.getNodeValue();
                QRLoginParam param = new QRLoginParam(name, humanName, type);
                param.setValue(value);
                this.params.put(name,param); 
            }
        
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    
    }

    public HashMap<String, QRLoginParam> getParams() {
        return params;
    }

    public List<QRLoginParam> getParamList(){
        return new ArrayList<QRLoginParam>(this.params.values());
    }

    public void setParams(HashMap<String, QRLoginParam> params) {
        this.params = params;
    }
    

}
